﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Serviio.API
{
    public class ServiioReferenceData
    {
        private const string endpoint = "/rest/refdata";

        private const string cpuCoresProperty = "cpu-cores";
        private const string profilesProperty = "profiles";
        private const string metadataLanguagesProperty = "metadataLanguages";
        private const string browsingCategoriesLanguagesProperty = "browsingCategoriesLanguages";
        private const string descriptiveMetadataExtractorsProperty = "descriptiveMetadataExtractors";
        private const string categoryVisibilityTypesProperty = "categoryVisibilityTypes";
        private const string onlineRepositoryTypesProperty = "onlineRepositoryTypes";
        private const string onlineContentQualitiesProperty = "onlineContentQualities";

        private ServiioConnection conn = null;

        public Dictionary<string, string> CpuCores { get; private set; }
        public Dictionary<string, string> Profiles { get; private set; }
        public Dictionary<string, string> MetadataLanguages { get; private set; }
        public Dictionary<string, string> BrowsingCategoriesLanguages { get; private set; }
        public Dictionary<string, string> DescriptiveMetadataExtractors { get; private set; }
        public Dictionary<string, string> CategoryVisibilityTypes { get; private set; }
        public Dictionary<int, string> OnlineMaxItemsChoices { get; private set; }
        public Dictionary<string, string> OnlineRepositoryTypes { get; private set; }
        public Dictionary<string, string> OnlineContentQualities { get; private set; }

        public ServiioReferenceData(ServiioConnection connection)
        {
            this.conn = connection;

            CpuCores = new Dictionary<string, string>();
            Profiles = new Dictionary<string, string>();
            MetadataLanguages = new Dictionary<string, string>();
            BrowsingCategoriesLanguages = new Dictionary<string, string>();
            DescriptiveMetadataExtractors = new Dictionary<string, string>();
            CategoryVisibilityTypes = new Dictionary<string, string>();
            OnlineRepositoryTypes = new Dictionary<string, string>();
            OnlineContentQualities = new Dictionary<string, string>();
            Refresh();

            OnlineMaxItemsChoices = new Dictionary<int, string>();
            OnlineMaxItemsChoices.Add(10, "10");
            OnlineMaxItemsChoices.Add(20, "20");
            OnlineMaxItemsChoices.Add(30, "30");
            OnlineMaxItemsChoices.Add(40, "40");
            OnlineMaxItemsChoices.Add(50, "50");
            OnlineMaxItemsChoices.Add(-1, "All");
        }

        public bool Refresh()
        {
            bool success = true;
            success = success && RefreshProperty(cpuCoresProperty, CpuCores);
            success = success && RefreshProperty(profilesProperty, Profiles);
            success = success && RefreshProperty(metadataLanguagesProperty, MetadataLanguages);
            success = success && RefreshProperty(browsingCategoriesLanguagesProperty, BrowsingCategoriesLanguages);
            success = success && RefreshProperty(descriptiveMetadataExtractorsProperty, DescriptiveMetadataExtractors);
            success = success && RefreshProperty(categoryVisibilityTypesProperty, CategoryVisibilityTypes);
            success = success && RefreshProperty(onlineRepositoryTypesProperty, OnlineRepositoryTypes);
            success = success && RefreshProperty(onlineContentQualitiesProperty, OnlineContentQualities);
            return success;
        }

        private bool RefreshProperty(string propertyName, Dictionary<string, string> propertyDict)
        {
            try
            {
                propertyDict.Clear();

                XmlDocument xDoc = RESTHelper.Get(conn.Host + endpoint + "/" + propertyName);
                XmlNodeList itemList = xDoc.SelectNodes("/refdata/values/item");
                foreach (XmlNode itemNode in itemList)
                {
                    if (itemNode.SelectSingleNode("name") != null)
                    {
                        string itemName = itemNode.SelectSingleNode("name").InnerText;
                        string itemValue = itemNode.SelectSingleNode("value").InnerText;

                        propertyDict.Add(itemName, itemValue);
                    }
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

    }
}
